package com.lzy.mall.order.listener;

import com.lzy.mall.common.constant.MQConstant;
import com.lzy.mall.order.entity.OmsOrder;
import com.lzy.mall.order.service.OmsOrderService;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * 监听过期订单，自动取消
 * @author lzy
 * @date 2021/10/12 13:29
 */
@Component
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@RabbitListener(queues = MQConstant.orderReleaseQueue)
@Slf4j
public class CloseOrderListener {

    private final OmsOrderService orderService;

    @RabbitHandler
    public void closeOrderHandler(OmsOrder order, Message message, Channel channel) throws Exception {
        System.out.println("库存关闭");
        try {
            orderService.closeOrder(order);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        } catch (Exception exception) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
